package com.xiaoyun.rollutil.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoyun.rollutil.entity.RubbishInfo;
import com.xiaoyun.rollutil.entity.bo.AimBO;
import com.xiaoyun.rollutil.entity.bo.RubbishBO;
import com.xiaoyun.rollutil.entity.bo.ApiModelBO;
import com.xiaoyun.rollutil.mapper.RubbishInfoMapper;
import com.xiaoyun.rollutil.service.IRubbishService;
import com.xiaoyun.rollutil.util.RequestUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: XiaoYun461
 * @Date: 2021/12/2 22:18
 */
@Service
@AllArgsConstructor
@Slf4j
public class RubbishServiceImpl implements IRubbishService {

    private final RubbishInfoMapper rubbishInfoMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public RubbishInfo getRubbishInfo(String name) {
        RubbishInfo rubbishInfo = selectByGoodsName(name);
        if (checkRubbishWhole(rubbishInfo)) {
            return rubbishInfo;
        }
        ApiModelBO apiModelBO = RequestUtil.getRubbishModel(name);
        if (!apiModelBO.isSuccess()) {
            log.info("请求错误 该垃圾为:{}, 描述信息为:{}", name, apiModelBO.getMsg());
            return null;
        }
        RubbishBO rubbishBO = JSONUtil.toBean((JSONObject) apiModelBO.getData(), RubbishBO.class);
        AimBO aim = rubbishBO.getAim();
        List<AimBO> recommendList = rubbishBO.getRecommendList();
        List<String> list = new ArrayList<>(recommendList.size());
        if (CollUtil.isEmpty(recommendList)) {
            for (AimBO aimBO : recommendList) {
                list.add(aimBO.getGoodsName() + "," + aimBO.getGoodsType());
                if (Objects.equals(aim.getGoodsName(), aimBO.getGoodsName())) {
                    continue;
                }
                RubbishInfo oldRubbishInfo = selectByGoodsName(aimBO.getGoodsName());
                if (checkRubbishWhole(oldRubbishInfo)) {
                    continue;
                }
                insertOrUpdate(oldRubbishInfo, aimBO, null);
            }
        }
        rubbishInfo = insertOrUpdate(rubbishInfo, aim, list);
        return rubbishInfo;
    }


    /**
     * 判断是否是否有主键, 有则更新.没有新增
     *
     * @param rubbishInfo
     * @param aim
     * @return
     */
    private RubbishInfo insertOrUpdate(RubbishInfo rubbishInfo, AimBO aim, List<String> similarly) {
        if (rubbishInfo == null) {
            rubbishInfo = new RubbishInfo();
            rubbishInfo.setSimilar(CollUtil.join(similarly, ";"));
            BeanUtils.copyProperties(aim, rubbishInfo);
            rubbishInfoMapper.insert(rubbishInfo);
        } else {
            if (CollUtil.isNotEmpty(similarly)) {
                rubbishInfo.setSimilar(CollUtil.join(similarly, ";"));
            }
            rubbishInfoMapper.updateById(rubbishInfo);
        }
        return rubbishInfo;
    }

    private RubbishInfo selectByGoodsName(String name) {
        return rubbishInfoMapper.selectOne(new LambdaQueryWrapper<RubbishInfo>()
                .eq(RubbishInfo::getGoodsName, name));
    }

    private boolean checkRubbishWhole(RubbishInfo rubbishInfo) {
        return rubbishInfo != null && StrUtil.isNotEmpty(rubbishInfo.getSimilar());
    }
}
